package com.it00zyq.admin.config;

import com.it00zyq.admin.result.HotArticleResult;
import com.it00zyq.admin.util.HotArticleUtil;
import com.it00zyq.common.config.RedisConfig;
import com.it00zyq.common.mapper.ArticleMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author zyq
 * @Date 2022/5/2 19:09
 * @Description TODO
 * @Version 1.0
 **/
@Component
@RequiredArgsConstructor
public class InitHotArticle implements CommandLineRunner {

    private final HotArticleUtil util;
    private final RedisTemplate<String, Object> template;

    @Override
    public void run(String... args) throws Exception {
        Map<Object, Object> entries = template.opsForHash().entries(RedisConfig.VIEW_KEY);
        for (Map.Entry<Object, Object> entry : entries.entrySet()) {
            if (RedisConfig.VIEW_TOTAL_KEY.equals(entry.getKey())) {
                continue;
            }
            util.insert(new HotArticleResult().setId(Integer.parseInt((String) entry.getKey())).setReadCount((Integer) entry.getValue()));
        }
    }
}
